home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / -archivi / -recent2 / gaplib.lha / GAPLib_Beta / genomes / BoundedRealVector.c < prev    next >
C/C++ Source or Header  |  1999-02-08  |  2KB  |  103 lines

  1. #include <math.h>
  2. #include <float.h>
  3. #include <stdio.h>
  4. #include <GAP.h>
  5.  
  6. /*
  7.  * Example bounded real-vector genome and functions for Crossover, Mutation,
  8.  * Comparing, Initizlization and Displaying.
  9.  *
  10.  * A bounded vector is a vector where the elements are constrained to
  11.  * specified ranges.
  12.  */
  13.  
  14. #define    VLENGTH    3    /* Number of elements in the vector. */
  15. #define    UCROSS    1    /* Crossover considers doubles to be unit length. */
  16.  
  17. /* Constaint ranges {Low,High} */
  18. /* Note! All ranges are inclusive. */
  19.  
  20. static double    Constraints[VLENGTH][2] = {
  21.     {-1.0,1.0},    /* v[0] */
  22.     {-1.0,1.0}, /* v[1] */
  23.     {-1.0,1.0}    /* v[2] */
  24. };
  25.  
  26. struct BRVPolyphant    {
  27.     double    v[VLENGTH];
  28. };
  29.  
  30. void BRVInit(struct BRVPolyphant * );
  31. void BRVMutate(struct BRVPolyphant * );
  32. void BRVCross(struct BRVPolyphant * , struct BRVPolyphant *);
  33. double BRVDiff(struct BRVPolyphant *,struct BRVPolyphant *);
  34. void BRVDisplay(struct BRVPolyphant * );
  35.  
  36.  
  37. void BRVInit(struct BRVPolyphant *Polly)
  38. {
  39. int i;
  40. for(i=0;i!=VLENGTH;i++) {
  41.     Polly->v[i] = InRand(Constraints[i][0],Constraints[i][1]);
  42. }
  43. }
  44.  
  45. void BRVMutate(struct BRVPolyphant *Polly)
  46. {
  47. int i;
  48.     if(Rnd(1024)<(VLENGTH)) {
  49.         i = Rnd(VLENGTH);
  50.         Polly->v[i] = InRand(Constraints[i][0],Constraints[i][1]);
  51.     }
  52. }
  53.  
  54. void BRVCross(struct BRVPolyphant *Polly, struct BRVPolyphant *Tweety)
  55. {
  56. int i;
  57. #ifdef    UCROSS
  58. double t;
  59. i = Rnd(VLENGTH+1);
  60. for(;i<VLENGTH;i++) {
  61.     t = Polly->v[i];
  62.     Polly->v[i] = Tweety->v[i];
  63.     Tweety->v[i] = t;
  64. }
  65. #else
  66. double    delta,dpos,avg;
  67.  
  68. for(i=0;i!=VLENGTH;i++) {
  69.     delta = fabs(Polly->v[i]-Tweety->v[i]);
  70.     if(delta>DBL_EPSILON) {
  71.         dpos = InRand(-delta,delta);
  72.         avg = (Polly->v[i]+Tweety->v[i])/2.0;
  73.         Polly->v[i] = avg+dpos;
  74.         Tweety->v[i] = avg-dpos;
  75.     }
  76. }
  77. #endif
  78. }
  79.  
  80. double BRVDiff(struct BRVPolyphant *Polly,struct BRVPolyphant *Tweety)
  81. {
  82. double    l=0,t;
  83. int i;
  84.  
  85. for(i=0;i!=VLENGTH;i++) {
  86.     t = Polly->v[i]-Tweety->v[i];
  87.     l += t*t;
  88. }
  89.  
  90. return(sqrt(l));
  91. }
  92.  
  93. void BRVDisplay(struct BRVPolyphant *Polly)
  94. {
  95. int i;
  96. printf("(%.3lf",Polly->v[0]);
  97. for(i=1;i<VLENGTH;i++) {
  98.     printf(",%.3lf",Polly->v[i]);
  99. }
  100. printf(")\n");
  101. }
  102.  
  103.